﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;

namespace QLTT.ViewModels
{
    class AllClassTimeViewModel:WorkspaceViewModel
    {
        private ObservableCollection<ClassTimeViewModel> _lstClassTimeViewModel;
        private icanDataContext _dataContext;
        private DelegateCommand _addCommand;
        private DelegateCommand _deleteCommand;
        private DelegateCommand _showClassCommand;
        private IQueryable<ClassTimeViewModel> _query;
        private string _classTimeName;
        public AllClassTimeViewModel(icanDataContext dataContext)
        {
            _lstClassTimeViewModel = new ObservableCollection<ClassTimeViewModel>();
            _dataContext = dataContext;
            base.DisplayName = Properties.Resources.Base_DisplayName_AllClassTime;

        }
        public AllClassTimeViewModel(icanDataContext dataContext, bool isGetAll)
        {
            _lstClassTimeViewModel = new ObservableCollection<ClassTimeViewModel>();
            _dataContext = dataContext;
            if (isGetAll)
            {
                _query = from classTime in _dataContext.ClassTimes select new ClassTimeViewModel(_dataContext, classTime);
                this.NumRow = _query.Count();
                this.CurPage = 1;
                this.RowsPerPage = 100;
                this.UpdateList();
            }
            base.DisplayName = Properties.Resources.Base_DisplayName_AllClassTime;
        }
        public string Name
        {
            get { return _classTimeName; }
            set { _classTimeName = value; }
        }
        
        public ObservableCollection<ClassTimeViewModel> AllClassTimes
        {
            get { return _lstClassTimeViewModel; }
            set {
                _lstClassTimeViewModel = value;
                base.OnPropertyChanged("AllClassTimes");
            }
        }
        
        public ICommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                {
                    _addCommand = new DelegateCommand(AddClassTime);
                }
                return _addCommand;
            }
        }
        public ICommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                {
                    _deleteCommand = new DelegateCommand(DeleteClassTime, CanDeleteClassTime);
                }
                return _deleteCommand;
            }
        }
        public ICommand ShowClassCommand
        {
            get
            {
                if (_showClassCommand == null)
                {
                    _showClassCommand = new DelegateCommand(ShowClass);
                }
                return _showClassCommand;
            }
        }
        

        public void AddClassTime()
        {
            ClassTimeViewModel ws = new ClassTimeViewModel(_dataContext);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, ws);
        }
        
        private void DeleteClassTime()
        {
            foreach (ClassTimeViewModel tvm in AllClassTimes)
            {
                if (tvm.IsSelected)
                {
                    tvm.Delete();
                }
            }
            this.NumRow = _query.Count();
            this.UpdateList();

        }
        private bool CanDeleteClassTime()
        {
            return ViewModelBase.CurrentRole == UserRole.Administrator && AllClassTimes.Count(ct => ct.IsSelected == true) > 0;
        }

        public void ShowClass()
        {
            ClassTimeViewModel tVM = this.AllClassTimes.First(t => t.IsSelected == true);
            if (tVM == null)
                return;
            AllClassViewModel cVM = new AllClassViewModel(_dataContext);
            cVM.ClassTime = tVM.ID;
            cVM.Find();
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, cVM);
        }
        
        [MediatorLib.MediatorMessageSink(Messages.ListClassTimeUpdated, ParameterType = typeof(ClassTime))]
        public void UpdateListClassTime(ClassTime classTime)
        {
            this.NumRow = _query.Count();
            this.UpdateList();
        }

        public override void UpdateList()
        {
            List<ClassTimeViewModel> lstClassTimes = _query.Skip((this.CurPage - 1) * this.RowsPerPage).Take(this.RowsPerPage).ToList();
            AllClassTimes = new ObservableCollection<ClassTimeViewModel>(lstClassTimes);
            base.UpdateList();
        }
    }
}
